package com.library.mapper;

import com.library.dto.ReviewDTO;
import com.library.entity.BookReviews;
import com.library.vo.BookReviewsVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface BookReviewsMapper {
    /**
     * 保存评论
     * @param bookReviews
     */
    @Insert("insert into book_reviews(user_id, book_id, username, book_title, content, rating, parent_review_id,create_time) " +
            "VALUES " +
            "(#{userId}, #{bookId},#{username}, #{bookTitle}, #{content}, #{rating}, #{parentReviewId},#{createTime})")
    void save(BookReviews bookReviews);

    /**
     * 根据评论id删除评论
     * @param reviewId
     */
    @Delete("delete from book_reviews where id = #{reviewId}")
    void deleteById(Long reviewId);

    /**
     * 根据评论id查询子评论数量
     * @param reviewId
     * @return
     */
    @Select("select count(*) from book_reviews where parent_review_id = #{reviewId}")
    int countByParentReviewId(Long reviewId);

    /**
     * 根据评论父评论id删除子评论
     * @param reviewId
     */
    @Delete("delete from book_reviews where parent_review_id = #{reviewId}")
    void deleteByParentReviewId(Long reviewId);

    /**
     * 根据图书id查询评论列表
     * @param bookId
     * @return
     */
    @Select("select * from book_reviews where book_id = #{bookId}")
    List<BookReviewsVO> getReviewsByBookId(Long bookId);

    /**
     * 根据父评论id查询子评论id
     * @param parentReviewId
     * @return
     */
    @Select("select id from book_reviews where parent_review_id = #{parentReviewId}")
    List<Long> selectChildReviewIds(Long parentReviewId);
}
